package bim.domain;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import keter.domain.base.BaseEntity;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonIgnore;


/**
 * <p>Class       : paper.domain.Message
 * <p>Descdription: 消息基类
 * @author  顾力行-gulixing@msn.com
 * @version 1.0.0
 */
@Entity
public  class Message extends BaseEntity  implements Cloneable{
    @NotEmpty(message="消息标题不能为空")
	private String title;
    
    private String position = Position.INBOX.toString();
	
    @NotEmpty(message="消息内容不能为空")
    private String content;
    
    @NotNull(message="发信人不能为空")
    private User sender;
    
    private User receiver;
    @ManyToOne
    @JoinColumn(name = "receiver_id")
    public User getReceiver() {
        return receiver;
    }
    public void setReceiver(User receiver) {
        this.receiver = receiver;
    }

    // 群发消息：需要记录接收者名称信息
    private String receiverNames;
    public String getReceiverNames() {
        return receiverNames;
    }
    public void setReceiverNames(String receiverNames) {
        this.receiverNames = receiverNames;
    }
    
    /*是否已读*/
	private boolean read = false;

    public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name="sender_id")//关联主键
	@JsonIgnore
	public User getSender() {
		return sender;
	}
	public void setSender(User sender) {
		this.sender = sender;
	}
	
	@Transient
	public String getSenderName(){
		return sender.getName();
	}
	
	@org.hibernate.annotations.Type(type="yes_no")
	@Column(name="is_read")
	public boolean getRead() {
		return read;
	}
	public void setRead(boolean read) {
		this.read = read;
	}
	
    public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	
	public static enum Position{
		SENT,  //已发消息 
		INBOX  //收到消息
	}
	
	   public Object clone() {
	        try {
	            return super.clone();
	        } catch (CloneNotSupportedException e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
}
